package com.ch.config;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ch.entity.User;
import com.ch.entity.appproprity;
import com.ch.service.UserService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class listenemq {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private  appproprity appproprity;
    @Autowired
    private UserService userService;
    @RabbitListener(queues = "${app.queuename}")
    public void listenSimpleQueueMessage(Map map) throws InterruptedException {

        User user;
        user = JSONUtil.toBean((String) map.get("user"),User.class);
        user.setId(0);
        userService.insert(user);
        User login = userService.login(user.getUserName(), user.getPwd());
        Map<String,Integer> d=new HashMap<>();
        d.put("id", (Integer) map.get("id"));
        d.put("uid",login.getId());
        System.out.println(d);
        rabbitTemplate.convertAndSend(appproprity.getQueuenamea(),d);
    }
}
